home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************************
- *
- *
- * ObjectMacZapp -- a standard Mac OOP application template
- *
- *
- *
- * ZExtraDialogItems.cpp -- some other kinds of dialog item objects
- *
- *
- * © 1997, Graham Cox
- *
- *
- *
- *************************************************************************************************/
-
- #include "ZExtraDialogItems.h"
- #include "ZClipboard.h"
- #include "MacZoop.h"
- #include "xDEFJump.h"
-
- #include <Palettes.h>
- #include <LowMem.h>
- #include <Icons.h>
-
- // embedded LDEF function:
-
- static pascal void IconListBoxDefProc( short iMessage,
- Boolean selected,
- Rect* bounds,
- Cell theCell,
- short dataOffset,
- short dataLength,
- ListHandle theList );
-
- static void DrawIconCell( Rect* r,
- IconInfo* info,
- Boolean hilited,
- Boolean showTitles,
- IconHilite hStyle );
-
-
- #pragma mark ====== ZLineDialogItem =======
-
- /*-------------------------------*** CONSTRUCTOR ***----------------------------------*/
-
- ZLineDialogItem::ZLineDialogItem( ZDialog* aDialog, short item )
- : ZDialogItem( aDialog, item )
- {
- itsType = plainLine;
- patIndex = 0;
- }
-
-
- /*---------------------------------*** DRAWITEM ***-----------------------------------*/
- /*
- draw the line item according to its parameters
- ----------------------------------------------------------------------------------------*/
-
- void ZLineDialogItem::DrawItem()
- {
- // draw the line using the style set. It is always 1 pixel wide (or 2 if 3D)
- // and the longer dimension of the bounds sets the orientation.
-
- RGBColor f, b, white = { 0xFFFF, 0xFFFF, 0xFFFF };
- Boolean isVert = (bounds.bottom - bounds.top) > (bounds.right - bounds.left);
-
- PenSize( 1, 1 );
-
- if ( itsType == patternLine )
- {
- Pattern p;
-
- GetIndPattern( &p, sysPatListID, patIndex);
- PenPat( &p );
- }
- else
- PenPat( &qd.black );
-
- PenMode( patCopy );
- MoveTo( bounds.left, bounds.top );
-
- // if a 3D line, establish the relevant colours based on the fore and back colours
- // set for the window.
-
- if ( itsType == raised3DLine ||
- itsType == etched3DLine )
- {
- GDHandle bestDev;
- Rect r = bounds;
-
- GetForeColor( &f );
- GetBackColor( &b );
-
- LocalToGlobal( &topLeft( r ));
- LocalToGlobal( &botRight( r ));
-
- bestDev = GetMaxDevice( &r );
-
- GetGray( bestDev, &b, &f );
- GetGray( bestDev, &white, &b );
-
- // draw the 3D lines. <f> is the darker colour, <b> is the lighter
-
- if ( isVert )
- {
- if (itsType == raised3DLine )
- RGBForeColor( &b );
- else
- RGBForeColor( &f );
-
- LineTo( bounds.left, bounds.bottom - 1 );
- Move( 1, 0 );
-
- if (itsType == raised3DLine )
- RGBForeColor( &f );
- else
- RGBForeColor( &b );
-
- LineTo( bounds.left + 1, bounds.top );
- }
- else
- {
- if (itsType == raised3DLine )
- RGBForeColor( &b );
- else
- RGBForeColor( &f );
-
- LineTo( bounds.right - 1, bounds.top );
- Move( 0, 1 );
-
- if (itsType == raised3DLine )
- RGBForeColor( &f );
- else
- RGBForeColor( &b );
-
- LineTo( bounds.left, bounds.top + 1 );
- }
- }
- else
- {
- // a plain or patterned line
-
- if ( isVert )
- LineTo( bounds.left, bounds.bottom - 1 );
- else
- LineTo( bounds.right - 1, bounds.top );
- }
-
- PenNormal();
- ForeColor( blackColor );
- }
-
-
- /*---------------------------------*** INITITEM ***-----------------------------------*/
- /*
- set up line parameters from original magic string params
- ----------------------------------------------------------------------------------------*/
-
- void ZLineDialogItem::InitItem( const long param1, const long param2 )
- {
- itsType = ( LineType ) param1;
- patIndex = param2;
- }
-
-
- #pragma mark -
- #pragma mark ======= ZIconListBox ========
-
-
- /*-------------------------------*** CONSTRUCTOR ***---------------------------------*/
-
- ZIconListBox::ZIconListBox( ZDialog* aDialog, short item )
- : ZListDialogItem( aDialog, item )
- {
- showTitles = TRUE;
- smallIcons = FALSE;
- iHilite = iconDefaultHilite;
- }
-
- /*--------------------------------*** DESTRUCTOR ***---------------------------------*/
-
- ZIconListBox::~ZIconListBox()
- {
- if ( theList )
- {
- // dispose icons
-
- DisposeIconCells();
-
- // get rid of the embedded LDEF handle if it's there
-
- Handle h = (*theList)->listDefProc;
-
- if ( h )
- {
- Cell c = { 0, 0 };
-
- IconListBoxDefProc( lCloseMsg, FALSE, &bounds, c, 0, 0, theList );
- DisposeHandle( h );
- }
- // important:
-
- (*theList)->listDefProc = NULL;
- }
- }
-
- /*---------------------------------*** INITITEM ***-----------------------------------*/
- /*
- set up icon listbox parameters from original magic string params
- ----------------------------------------------------------------------------------------*/
-
- void ZIconListBox::InitItem( const long param1, const long param2 )
- {
- // make the list
-
- MakeMacList( param1 );
-
- // install the icons from the template
-
- if ( param1 > 0 )
- AddIconCells( param1 );
-
- // do the usual trick of sizing the bounds to encompass a whole number
- // of cells
-
- short cellHeight = (*theList)->cellSize.v;
- short cellWidth = (*theList)->cellSize.h;
- short adjustAmount;
- Boolean hasH, hasV;
-
- hasH = ((*theList)->hScroll != NULL);
- hasV = ((*theList)->vScroll != NULL);
-
- adjustAmount = (bounds.bottom - bounds.top - (hasH? 17 : 2)) % cellHeight;
- bounds.bottom -= adjustAmount;
-
- adjustAmount = (bounds.right - bounds.left - (hasV? 17 : 2)) % cellWidth;
- bounds.right -= adjustAmount;
-
- LSize( bounds.right - bounds.left - (hasV? 17 : 2),
- bounds.bottom - bounds.top - (hasH? 17 : 2),
- theList );
-
- // ready to go, so activate the list appropriately
-
- LActivate( TRUE, theList );
- ActivateListItem( FALSE );
- }
-
- /*-------------------------------*** MAKEMACLIST ***----------------------------------*/
- /*
- build the icon list structures. This is a normal List Manager list but using a custom
- LDEF that we have embedded within this source file. Each cell of the list contains an
- icon and optionally a title string below it. Layout can be horizontal or vertical.
- ----------------------------------------------------------------------------------------*/
-
- void ZIconListBox::MakeMacList( const short listTemplateID )
- {
- Point cellSize = { 34, 0 };
- ListTemplateHdl ltH;
- IconListBoxHdl lbH;
- Rect r, dataBounds = { 0, 0, 0, 1 };
- Boolean hasV = TRUE, hasH = FALSE;
- ZDialog* zd = (ZDialog*) GetBoss();
-
- // get the bounding box for this item
-
- r = bounds;
- InsetRect( &r, 1, 1 );
-
- // see if there's a LIST template, which will define our font, etc
-
- if ( listTemplateID != 0 )
- {
- ltH = ( ListTemplateHdl ) GetResource( kListTemplateResType, listTemplateID );
-
- if ( ltH )
- {
- // there is a template, so use that to define the list.
-
- dataBounds.right = (*ltH)->columns;
- dataBounds.bottom = (*ltH)->rows;
- hasV = (*ltH)->hasVertScroll;
- hasH = (*ltH)->hasHorizScroll;
- canBeHandler = (*ltH)->keyboardNav;
- fontSize = (*ltH)->fontSize;
- strListID = (*ltH)->stringsListID;
-
- GetFNum((*ltH)->fontName, &font );
-
- ReleaseResource((Handle) ltH );
- }
-
- // get some basic settings from the ICLB resource
-
- lbH = (IconListBoxHdl) GetResource( kIconListTemplateResType, listTemplateID );
-
- if ( lbH )
- {
- showTitles = (*lbH)->addTitles;
- iHilite = (IconHilite)(*lbH)->hiliteStyle;
-
- ReleaseResource((Handle) lbH );
- }
- }
- // allow space for the scrollbars we want
-
- if ( hasV )
- r.right -= 15;
-
- if ( hasH )
- r.bottom -= 15;
-
- // compute the cell height based on the size of an icon plus the current font
-
- if ( showTitles )
- {
- FontInfo fi;
-
- GetFontInfo( &fi );
- cellSize.v = 34 + fi.ascent + fi.descent + fi.leading;
- }
-
- if ( hasH )
- cellSize.h = 64;
-
- // make the list initally
-
- short saveFont, saveSize;
-
- SetUpFontForList( &saveFont, &saveSize );
-
- theList = LNew( &r, &dataBounds, cellSize, 0, zd->GetMacWindow(), TRUE, FALSE, hasH, hasV );
- FailNIL( theList );
-
- // now scrungle our cunning LDEF into it
-
- InstallIconLDEF();
-
- // now we just need to install all our icons, but we leave that to InitItem, which called
- // this method in the first place
-
- if (font != 0 &&
- fontSize != 0 )
- {
- TextFont( saveFont );
- TextSize( saveSize );
- }
-
- // by default, icon lists only allow one selection at a time (probably most common use)
- // change this line or override for other behaviours.
-
- (*theList)->selFlags = lOnlyOne;
- }
-
-
- /*-----------------------------*** INSTALLICONLDEF ***--------------------------------*/
- /*
- set the list to use our embedded LDEF and make sure it's inited properly
- ----------------------------------------------------------------------------------------*/
-
- void ZIconListBox::InstallIconLDEF()
- {
- Handle ldefHand;
- Rect r = {0, 0, 0, 0};
- Cell aCell = { 0, 0 };
-
- FailNIL( ldefHand = GetUniversalFunctionHandle((ProcPtr) IconListBoxDefProc, uppListDefProcInfo ));
-
- (*theList)->listDefProc = ldefHand;
-
- // set the list's refcon to this so that the LDEF can callback into us if need be
-
- (*theList)->refCon = (long) this;
-
- // the "init" message won't have been sent, since it was inited using the system LDEF,
- // so simply call it directly in case the LDEF does something here.
-
- IconListBoxDefProc( lInitMsg, FALSE, &r, aCell, 0, 0, theList );
- }
-
-
- /*-------------------------------*** ADDICONCELLS ***---------------------------------*/
- /*
- read the icon listbox template and fetch all the icons, etc needed and set them up
- ----------------------------------------------------------------------------------------*/
-
- void ZIconListBox::AddIconCells( const short templateID )
- {
- short i;
- Cell c = { 0, 0 };
- IconInfo info;
- Boolean isHList;
- IconListBoxHdl lbH;
-
- isHList = ((*theList)->hScroll != NULL) && ((*theList)->vScroll == NULL);
-
- // attempt to locate the template (ICLB) with the ID passed
-
- lbH = (IconListBoxHdl) GetResource( kIconListTemplateResType, templateID );
-
- if ( lbH )
- {
- // obtained the template, now construct the icon list
-
- for( i = 0; i < (*lbH)->numIcons; i++ )
- {
- // locate each icon and load it as needed
-
- info.iconID = (*lbH)->icons[i].resID;
-
- switch ((*lbH)->icons[i].iconType)
- {
- case 0: // plain black & white icon
- info.iType = IconPlain;
- info.theIcon = GetIcon( info.iconID );
- break;
- case 1: // 'cicn' resource
- info.iType = IconColour;
- info.theIcon = (Handle) GetCIcon( info.iconID );
- break;
- case 2: // indexed icon, etc
- default:
- info.iType = IconIndexed;
- info.theIcon = NULL;
- break;
- }
-
- // get the title string from the relevant string list
-
- GetIndString( info.title, (*lbH)->titleListResID, (*lbH)->icons[i].nameIndex );
-
- // to make the titles look nicer when hilited, we add a space in front and another
- // at the end of the title
-
- BlockMoveData( &info.title[1], &info.title[2], info.title[0] );
- info.title[1] = ' ';
- info.title[0] += 2;
- info.title[ info.title[0]] = ' ';
- info.userData = 0;
-
- // add the resulting nugget of info to the list
-
- if (isHList)
- c.h = LAddColumn( 1, c.h + 1, theList );
- else
- c.v = LAddRow( 1, c.v + 1, theList );
-
- LSetCell( &info, sizeof( IconInfo ), c, theList );
- }
- ReleaseResource((Handle) lbH );
- }
- }
-
- /*-----------------------------*** DISPOSEICONCELLS ***-------------------------------*/
- /*
- disposes the icons when the list is closed
- ----------------------------------------------------------------------------------------*/
-
- void ZIconListBox::DisposeIconCells()
- {
- Cell c = {0, 0};
- short row, col, len;
- IconInfo info;
-
- for( col = 0; col < (*theList)->dataBounds.right; col++ )
- {
- for( row = 0; row < (*theList)->dataBounds.bottom; row++ )
- {
- c.h = col;
- c.v = row;
- len = sizeof( IconInfo );
-
- LGetCell( &info, &len, c, theList );
-
- // determine the type of icon and release it
-
- if ( info.theIcon )
- {
- switch( info.iType )
- {
- case IconPlain:
- ReleaseResource( info.theIcon );
- break;
- case IconColour:
- DisposeCIcon((CIconHandle) info.theIcon );
- break;
- case IconSuite:
- DisposeIconSuite( info.theIcon, TRUE );
- break;
- }
- }
- }
- }
- }
-
- #pragma mark -
- #pragma mark ======= Embedded LDEF ======
-
-
- /*----------------------------*** IconListBoxDefProc ***------------------------------*/
-
- static pascal void IconListBoxDefProc( short iMessage,
- Boolean selected,
- Rect* bounds,
- Cell theCell,
- short dataOffset,
- short dataLength,
- ListHandle theList )
- {
- ZIconListBox* zb = (ZIconListBox*)(*theList)->refCon;
- IconInfo info;
-
- switch( iMessage )
- {
- case lInitMsg:
- break;
- case lDrawMsg:
- case lHiliteMsg:
- // set up a pointer to the list's cell data
-
- LGetCell( &info, &dataLength, theCell, theList );
- DrawIconCell( bounds,
- &info,
- selected,
- zb->ShowTitles(),
- zb->GetHiliteStyle());
-
- break;
- case lCloseMsg:
- break;
- }
- }
-
-
-
- static void DrawIconCell( Rect* r, IconInfo* info, Boolean hilited, Boolean showTitles, IconHilite hStyle )
- {
- // draw the icon in the rectangle, applying information from info, etc
-
- Rect ir = { 0, 0, 32, 32 };
- short iconTransform;
-
- // centre the icon's rect horizontally in the cell
-
- OffsetRect( &ir, ((r->left + r->right) / 2) - (( ir.right - ir.left ) / 2), r->top + 1 );
-
- // set up the icon transform based on the selection options set
-
- if (hilited && (hStyle & iconHiliteDarken))
- iconTransform = ttSelected;
- else
- iconTransform = ttNone;
-
- // plot the icon according to its type
-
- switch ( info->iType )
- {
- case IconIndexed:
- PlotIconID( &ir, atNone, iconTransform, info->iconID );
- break;
- case IconPlain:
- if (info->theIcon)
- {
- LoadResource( info->theIcon );
- PlotIcon( &ir, info->theIcon );
-
- if (hilited)
- InvertRect( &ir );
- }
- break;
- case IconColour:
- if (info->theIcon)
- {
- // for these icons, the rect may not be 32 x 32, so we need to get the real
- // size and centre it accordingly.
-
- CIconHandle ci = (CIconHandle) info->theIcon;
-
- ir = (*ci)->iconPMap.bounds;
- OffsetRect( &ir, -ir.left, -ir.top );
- OffsetRect( &ir, ((r->left + r->right) / 2) - (( ir.right - ir.left ) / 2),
- r->top + 33 - ir.bottom - ir.top );
-
- PlotCIconHandle( &ir, atNone, iconTransform, ci );
- }
- break;
- }
-
- // if we want bold border as well, draw it. Actually, this is pretty dismal looking
- // and I can't imagine why anyone would want it! You have been warned!
-
- if ( hStyle & iconHiliteBoldBorder )
- {
- InsetRect( &ir, -4, -4 );
- PenSize( 3, 3 );
-
- if (hilited)
- PenMode( patOr );
- else
- PenMode( patBic );
-
- FrameRect( &ir );
- }
-
- // draw the icon title if required
-
- if ( showTitles )
- {
- FontInfo fi;
- GetFontInfo( &fi );
-
- short tLen = StringWidth( info->title );
-
- MoveTo(((r->left + r->right) / 2) - (tLen / 2), r->top + 33 + fi.ascent );
-
- if (hilited && ( hStyle & iconHiliteInvertTitle))
- TextMode( notSrcCopy );
- else
- TextMode( srcCopy );
-
- DrawString( info->title );
- TextMode( srcOr );
- }
- }
-
- #pragma mark -
- #pragma mark ===== ZScrollingTextBox =====
-
-
- static pascal void ScrollProc( ControlHandle ch, short partCode );
-
- static ControlActionUPP gSBarActionUPP = NewControlActionProc( ScrollProc );
-
- /*-------------------------------*** CONSTRUCTOR ***----------------------------------*/
-
-
- ZScrollingTextBox::ZScrollingTextBox( ZDialog* aDialog, short item )
- : ZDialogItem( aDialog, item )
- {
- te = NULL;
- scroll = NULL;
- editable = FALSE;
- resID = 0;
- }
-
- /*-------------------------------*** DESTRUCTOR ***-----------------------------------*/
-
-
- ZScrollingTextBox::~ZScrollingTextBox()
- {
- if ( te )
- TEDispose( te );
-
- if ( scroll )
- DisposeControl( scroll );
- }
-
- /*---------------------------------*** INITITEM ***-----------------------------------*/
- /*
- set up the textbox item
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::InitItem( const long param1, const long param2 )
- {
- resID = LoWord( param1 );
- editable = ( param2 != 0 );
-
- // get the lineheight and page height of the text
-
- FontInfo fi;
-
- GetFontInfo( &fi );
- lineHeight = fi.ascent + fi.descent + fi.leading;
- pageHeight = bounds.bottom - bounds.top - lineHeight;
-
- // construct the mac stuff
-
- MakeMacTEAndScroll();
-
- // load the initial text
-
- PreloadText();
-
- // if the text is ediable, we can be the handler too
-
- canBeHandler = editable;
- }
-
- /*---------------------------------*** DRAWITEM ***-----------------------------------*/
- /*
- refresh the item
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::DrawItem()
- {
- TEUpdate( &bounds, te );
- FrameRect( &bounds );
-
- #ifdef _GREYSCALE_APPEARANCE
-
- FrameGrayRect( &bounds );
-
- #endif
- }
-
- /*---------------------------------*** CLICKITEM ***----------------------------------*/
- /*
- handle mouse click in item
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::ClickItem( const Point where, const short modifiers )
- {
- if ( enabled )
- {
- // make sure this is the handler when clicked
-
- if (! isHandler && canBeHandler)
- BecomeHandler( TRUE );
-
- // detect and handle click in scrollbar
-
- if ( PtInScrollbar( where ))
- {
- short partCode;
- ControlHandle ch;
- ZDialog* zd = (ZDialog*) GetBoss();
-
- partCode = FindControl( where, zd->GetMacWindow(), &ch );
-
- if ( partCode > 0 )
- {
- if ( partCode == kControlIndicatorPart )
- {
- short delta = GetControlValue( scroll );
-
- partCode = TrackControl( scroll, where, NULL );
-
- if ( partCode == kControlIndicatorPart )
- {
- delta -= GetControlValue( scroll );
-
- Scroll( delta );
- }
- }
- else
- partCode = TrackControl( scroll, where, gSBarActionUPP );
- }
- }
- else
- {
- if ( editable )
- {
- TEClick( where, modifiers & shiftKey, te );
- CalScroll();
- }
- }
- }
- }
-
- /*-------------------------------*** ADJUSTCURSOR ***---------------------------------*/
- /*
- set cursor to i-beam over editable text
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::AdjustCursor( const Point where, const short modifiers )
- {
- if ( PtInScrollbar( where ))
- InitCursor();
- else
- {
- if ( editable )
- {
- CursHandle ch = GetCursor( iBeamCursor );
-
- SetCursor( *ch );
- }
- }
- }
-
- /*---------------------------------*** ACTIVATE ***-----------------------------------*/
- /*
- activate/deactivate the text
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::Activate( const Boolean isActive )
- {
- ZAdvancedDialog* zd = (ZAdvancedDialog*) GetBoss();
-
- if ( isHandler && zd->HasMultipleFoci())
- DrawBorder( isActive );
-
- if ( editable )
- {
- if ( isActive )
- TEActivate( te );
- else
- TEDeactivate( te );
- }
- }
-
- /*-----------------------------*** BECOMEHANDLER ***----------------------------------*/
- /*
- become the current keyboard handler
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::BecomeHandler( Boolean isBecoming )
- {
- inherited::BecomeHandler( isBecoming );
-
- if ( editable )
- Activate( isHandler );
- }
-
-
- /*----------------------------------*** DOCUT ***-------------------------------------*/
- /*
- handle the Cut command
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::DoCut()
- {
- if ( editable )
- {
- TECut( te );
- CalScroll();
- }
- }
-
- /*----------------------------------*** DOCOPY ***------------------------------------*/
- /*
- handle the Copy command
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::DoCopy()
- {
- if ( editable )
- TECopy( te );
- }
-
- /*---------------------------------*** DOPASTE ***------------------------------------*/
- /*
- handle the Paste command
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::DoPaste()
- {
- if ( editable )
- {
- TEPaste( te );
- CalScroll();
- }
- }
-
- /*---------------------------------*** DOCLEAR ***------------------------------------*/
- /*
- handle the Clear command
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::DoClear()
- {
- if ( editable )
- {
- TEDelete( te );
- CalScroll();
- }
- }
-
-
- /*-------------------------------*** DOSELECTALL ***----------------------------------*/
- /*
- handle the Select All command
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::DoSelectAll()
- {
- if ( editable )
- TESetSelect( 0, 32767, te );
- }
-
-
- /*-------------------------------*** CANPASTETYPE ***---------------------------------*/
- /*
- see if there is pasteable data on the clipboard- we can handle TEXT.
- ----------------------------------------------------------------------------------------*/
-
- Boolean ZScrollingTextBox::CanPasteType()
- {
- return gClipboard->QueryType( 'TEXT' );
- }
-
-
- /*-------------------------------*** UPDATEMENUS ***----------------------------------*/
- /*
- update edit menu commands for this item where appropriate
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::UpdateMenus()
- {
- inherited::UpdateMenus();
-
- MenuHandle mH;
-
- if ( editable )
- gMenuBar->EnableCommand( kCmdSelectAll );
- }
-
-
- /*-----------------------------------*** TYPE ***-------------------------------------*/
- /*
- type text if editable
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::Type( const char theKey )
- {
- inherited::Type( theKey );
-
- if ( editable && ( theKey != 0x09 ))
- {
- TEKey( theKey, te );
- CalScroll();
- }
- }
-
- /*-----------------------------------*** IDLE ***-------------------------------------*/
- /*
- blink cursor in editable text
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::Idle()
- {
- if ( editable )
- TEIdle( te );
-
- inherited::Idle();
- }
-
- /*----------------------------------*** SETTEXT ***-----------------------------------*/
- /*
- set the item's text
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::SetText( Handle textH, Handle styleH )
- {
- FailOSErr(( textH == NULL )? paramErr : noErr );
-
- long tLen = GetHandleSize( textH );
-
- TEActivate( te );
- TESetSelect( 0, 32767, te );
- TEDelete( te );
-
- HLock( textH );
- TEStyleInsert( *textH, tLen, (StScrpHandle) styleH, te );
- HUnlock( textH );
-
- TESetSelect( 0, 0, te );
- TEDeactivate( te );
-
- CalScroll();
- }
-
- /*----------------------------------*** GETTEXT ***-----------------------------------*/
- /*
- get the text. This copies the text (and optionally the style) into the handles passed,
- resizing them as needed and replacing any existing data therein.
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::GetText( Handle textH, Handle styleH )
- {
- FailOSErr(( textH == NULL )? paramErr : noErr );
-
- char hs = HGetState((*te)->hText);
- StScrpHandle st = NULL;
- short se, ss;
-
- HLock((*te)->hText);
-
- try
- {
- FailOSErr( PtrToXHand( *(*te)->hText, textH, (*te)->teLength ));
-
- // if the styleH is not NULL, get the style handle too
-
- if ( styleH )
- {
- se = (*te)->selEnd;
- ss = (*te)->selStart;
-
- TESetSelect( 0, 32767, te );
-
- st = TEGetStyleScrapHandle( te );
-
- if ( st )
- {
- HLock((Handle) st );
-
- FailOSErr( PtrToXHand( *st, styleH, GetHandleSize((Handle) st )));
-
- HUnlock((Handle) st );
- DisposeHandle((Handle) st );
- }
-
- TESetSelect( ss, se, te );
- }
- }
- catch( OSErr err )
- {
- HSetState((*te)->hText, hs );
-
- if ( st )
- {
- DisposeHandle((Handle) st );
- TESetSelect( ss, se, te );
- }
-
- throw err;
- }
-
- HSetState((*te)->hText, hs );
- }
-
-
- /*---------------------------*** MAKEMACTEANDSCROLL ***-------------------------------*/
- /*
- create the textedit record and scrollbar
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::MakeMacTEAndScroll()
- {
- Rect rView;
- ZDialog* zd;
- WindowPtr w;
-
- zd = (ZDialog*) GetBoss();
- w = zd->GetMacWindow();
-
- rView = bounds;
- rView.right -= 15; // allow for scrollbar's width
- InsetRect( &rView, 3, 3 );
-
- FailNIL( te = TEStyleNew( &rView, &rView ));
-
- // make the scrollbar
-
- rView = bounds;
- rView.left = rView.right - 16;
-
- FailNIL( scroll = NewControl( w, &rView, NULL, TRUE, 0, 0, 0, scrollBarProc, (long) this ));
-
- TEAutoView( TRUE, te );
- }
-
- /*-------------------------------*** PRELOADTEXT ***----------------------------------*/
- /*
- install text from resource
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::PreloadText()
- {
- Handle t, s;
-
- if ( resID > 0 )
- {
- t = GetResource( 'TEXT', resID );
-
- if ( t )
- {
- s = GetResource( 'styl', resID );
-
- SetText( t, s );
-
- if ( s )
- ReleaseResource ( s );
-
- ReleaseResource( t );
- }
- }
- }
-
-
- /*--------------------------------*** CALSCROLL ***-----------------------------------*/
- /*
- adjust scrollbar max according to amount of text and height of box, etc.
- ----------------------------------------------------------------------------------------*/
-
- void ZScrollingTextBox::CalScroll()
- {
- short tHeight = TEGetHeight( 32767, 0, te );
- short sMax;
-
- // scrollbar max is set to the height of the lines less the height of bounds
-
- sMax = tHeight - ( bounds.bottom - bounds.top - 6 );
-
- if ( sMax < 0 )
- sMax = 0;
-
- SetControlMaximum( scroll, sMax );
-
- // compute value
-
- sMax = (*te)->viewRect.top - (*te)->destRect.top;
-
- SetControlValue( scroll, sMax );
- }
-
-
- Boolean ZScrollingTextBox::PtInScrollbar( const Point mouse )
- {
- Rect r = bounds;
-
- r.left = r.right - 15;
-
- return PtInRect( mouse, &r );
- }
-
-
-
- void ZScrollingTextBox::Scroll( short delta )
- {
- TEScroll( 0, delta, te );
- }
-
-
- void ZScrollingTextBox::DoScroll( short partCode )
- {
- short sDelta = 0;
-
- switch ( partCode )
- {
- case kControlUpButtonPart:
- sDelta = -lineHeight;
- break;
- case kControlDownButtonPart:
- sDelta = lineHeight;
- break;
- case kControlPageUpPart:
- sDelta = -pageHeight;
- break;
- case kControlPageDownPart:
- sDelta = pageHeight;
- break;
- }
-
- short sv = GetControlValue( scroll );
-
- SetControlValue( scroll, sv + sDelta );
- sDelta = sv - GetControlValue( scroll );
-
- Scroll( sDelta );
- }
-
-
- static pascal void ScrollProc( ControlHandle ch, short partCode )
- {
- ZScrollingTextBox* zi = (ZScrollingTextBox*) GetControlReference( ch );
-
- if ( zi )
- zi->DoScroll( partCode );
- }